/* 通过mutation间接更新state的多个方法对象 */
import {
    RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS, RESET_USER_INFO, RECEIVE_GOODS, RECEIVE_RATINGS, RECEIVE_INFO,
    INCREMENT_FOOD_COUNT, DECREMENT_FOOD_COUNT, CLEAR_CART, RECEIVE_SEARCH_SHOPS
} from './mutation_type'
import { reqAddress, reqFoodCategorys, reqShops, reqUserInfo, reqLogout, reqShopGoods, reqShopRatings, reqShopInfo ,reqSearchShop } from '../api'
export default {
    //异步获取地址
    async getAddress({ commit, state }) {
        // 发送异步ajax请求
        const geohash = state.latitude + ',' + state.longitude
        const result = await reqAddress(geohash)
        // 提交一个mutation
        if (result.code === 0) {
            const address = result.data
            commit(RECEIVE_ADDRESS, { address })
        }
    },

    // 异步获取食品分类列表
    async getFoodCategorys({ commit }) {
        // 发送异步ajax请求
        const result = await reqFoodCategorys()
        // 提交一个mutation
        if (result.code === 0) {
            const categorys = result.data
            commit(RECEIVE_CATEGORYS, { categorys })
        }
    },

    // 异步获取商家列表
    async getShops({ commit, state }) {
        // 发送异步ajax请求
        // const longitude = state.longitude
        // const latitude = state.latitude
        const { longitude, latitude } = state
        const result = await reqShops(longitude, latitude)
        // 提交一个mutation
        if (result.code === 0) {
            const shops = result.data
            commit(RECEIVE_SHOPS, { shops })
        }
    },

    // 同步记录登录信息
    recordUser({ commit, userInfo }) {
        commit(RECEIVE_USER_INFO, { userInfo })

    },
    // 异步获取登录信息 
    async reqUserInfo({ commit }) {
        let result = await reqUserInfo()//会话获取用户信息
        if (result.code === 0) {
            let userInfo = result.data
            commit(RECEIVE_USER_INFO, { userInfo })
        }
    },
    //异步登出 退出登录 重置用户信息
    async logout({ commit }) {
        let result = await reqLogout()
        if (result.code === 0) {
            commit(RESET_USER_INFO)
        }
    },

    //异步获取商家商品列表
    async getShopGoods({ commit }, callback) {
        let result = await reqShopGoods()
        if (result.code === 0) {
            const goods = result.data
            commit(RECEIVE_GOODS, { goods })
            callback && callback()
        }
    },
    //异步获取商家评价列表
    async getShopRatings({ commit }, callback) {
        let result = await reqShopRatings()
        if (result.code === 0) {
            const ratings = result.data
            commit(RECEIVE_RATINGS, { ratings })
            callback && callback()
        }
    },
    //异步获取商家信息
    async getShopInfo({ commit }) {
        let result = await reqShopInfo()
        if (result.code === 0) {
            const info = result.data
            commit(RECEIVE_INFO, { info })
        }
    },
    //同步更新商品数量
    updateFoodCount({ commit }, { isAdd, food }) {
        if (isAdd) {
            commit(INCREMENT_FOOD_COUNT, { food })
        } else {
            commit(DECREMENT_FOOD_COUNT, { food })
        }
    },
    //同步清除购物车
    clearCart({ commit }) {
        commit(CLEAR_CART)
    },

    // 异步获取商家列表
    async searchShops({ commit, state },keyword) {
        // 发送异步ajax请求
        const geohash = state.latitude + ',' + state.longitude
        const result = await reqSearchShop(geohash, keyword)
        // 提交一个mutation
        if (result.code === 0) {
            const searchShops = result.data
            commit(RECEIVE_SEARCH_SHOPS, { searchShops })
        }
    },
}